package com.rain.common.utils;

import ch.qos.logback.core.db.dialect.DBUtil;
import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author zhuxiaoke
 * @ClassName: DataSourceUtil
 * @Description: TODO
 * @date 2021/8/26 16:13
 */
public class DataSourceUtil {
    public static Connection getConnection(){
        Connection conn = null;

        try {
            conn = getDataSource().getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return conn;
    }

    public static DataSource getDataSource(){
        DataSource dataSource=null;
        Properties p = new Properties();
        try {
            p.load(DBUtil.class.getClassLoader().getResourceAsStream("dbconfig.properties"));
            dataSource = BasicDataSourceFactory.createDataSource(p);
        } catch (Exception e) {
            throw new RuntimeException("获取DataSource对象失败");
        }
        return dataSource;
    }
}
